package com.warehouse.service;

import com.warehouse.dao.WarehouseDao;
import com.warehouse.dao.impl.WarehouseDaoImpl;
import com.warehouse.entity.Cargo;
import com.warehouse.entity.Warehouse;
import com.warehouse.utils.Page;

import java.util.List;

public class WarehouseService {
    private WarehouseDao warehouseDao = new WarehouseDaoImpl();

    public List<Warehouse> queryPage() {
        return warehouseDao.all();
    }

    public int add(Warehouse warehouse) {
        return warehouseDao.add(warehouse);
    }

    public int update(Warehouse warehouse) {
        return warehouseDao.update(warehouse);
    }


    public int delete(int id) {
        return warehouseDao.delete(id);
    }

    public Warehouse byId(int id) {
        return warehouseDao.byId(id);
    }

    public Page<Warehouse> byCardNum(int start, int pageSize, String area) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = warehouseDao.countByAdminNum(area);//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Warehouse> cargos = warehouseDao.byAdminNum(startPage, pageSize, area);
        return new Page<Warehouse>(start, totalPage, pageSize, cargos);
    }

}
